package cn.maihe.elg.operation.web.bank;

import cn.maihe.elg.operation.repository.entity.SpdBankNotice;
import cn.maihe.elg.operation.repository.entity.SpdBankNoticeCondition;
import cn.maihe.elg.operation.schedule.CiticBankCardSchedule;
import cn.maihe.elg.operation.service.order.SpdBankNoticeService;
import cn.maihe.elg.operation.supports.bank.service.CiticBankCardService;
import cn.maihe.elg.operation.utils.ElgLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/bank/citic/card")
@Slf4j
public class CiticBankCardController {

    @Resource
    private CiticBankCardService citicBankCardService;
    @Resource
    private SpdBankNoticeService spdBankNoticeService;
    @Resource
    private CiticBankCardSchedule citicBankCardSchedule;

    /**
     * 为指定订单申请一张新的收款电子卡
     * @return 包含新卡号的响应
     */
    @PostMapping("/open/{acceptOrderNo}")
    public Map<String, Object> openNewCardForOrder(@PathVariable("acceptOrderNo") String acceptOrderNo) {
        Map<String, Object> result = new HashMap<>();
        try {
            ElgLogger.info(log, l -> l.info("接收到为订单ID [{}] 的开卡请求", acceptOrderNo));

            String newCardNo = citicBankCardService.openNewCardAndUpdateOrder(acceptOrderNo);

            result.put("success", true);
            result.put("message", "开卡成功");

            Map<String, String> data = new HashMap<>();
            data.put("acceptOrderNo", String.valueOf(acceptOrderNo));
            data.put("newCardNo", newCardNo);
            result.put("data", data);

        } catch (Exception e) {
            ElgLogger.error(log, l -> l.error("为订单ID [{}] 开卡时发生异常: {}", acceptOrderNo, e.getMessage(), e));
            result.put("success", false);
            result.put("message", "开卡失败: " + e.getMessage());
        }
        return result;
    }
    /**
     * 手动触发指定虚拟卡的流水查询
     * @param cardNo 虚拟卡号
     * @return 操作结果
     */
    @PostMapping("/manualDetails/{cardNo}")
    public Map<String, Object> syncCardDetails(@PathVariable String cardNo) {
        Map<String, Object> result = new HashMap<>();
        try {
            ElgLogger.info(log, l -> l.info("接收到手动同步卡 [{}] 流水的请求", cardNo));
            String today = java.time.LocalDate.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyyMMdd"));

            citicBankCardService.queryAndSaveCardTransactions(cardNo, today, today);

            result.put("success", true);
            result.put("message", "手动同步任务已触发并执行成功");

        } catch (Exception e) {
            ElgLogger.error(log, l -> l.error("手动同步卡 [{}] 流水时发生异常: {}", cardNo, e.getMessage(), e));
            result.put("success", false);
            result.put("message", "手动同步失败: " + e.getMessage());
        }
        return result;
    }
    /**
     * @param cardNo 虚拟卡号
     * @return 包含流水列表的响应
     */
    @GetMapping("/details/{cardNo}")
    public Map<String, Object> getSavedCardDetails(@PathVariable String cardNo) {
        Map<String, Object> result = new HashMap<>();
        try {
            ElgLogger.info(log, l -> l.info("接收到查询卡 [{}] 已存流水的请求", cardNo));
            SpdBankNoticeCondition condition = SpdBankNoticeCondition.builder()
                    .andSubAccountEq(cardNo)
                    .andNoticeTypeEq("CITIC_CARD");
            condition.orderDesc("gmt_create");
            List<SpdBankNotice> notices = spdBankNoticeService.selectList(condition.build());

            result.put("success", true);
            result.put("message", "查询成功");
            result.put("data", notices);
            result.put("count", notices.size());

        } catch (Exception e) {
            ElgLogger.error(log, l -> l.error("查询卡 [{}] 已存流水时发生异常: {}", cardNo, e.getMessage(), e));
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * @return 操作结果
     */
    @PostMapping("/triggerSchedule/query")
    public Map<String, Object> triggerQueryActiveCardsSchedule() {
        Map<String, Object> result = new HashMap<>();
        try {
            ElgLogger.info(log, l -> l.info("接收到手动触发[查询活跃卡流水]定时任务的请求"));

            // 直接调用定时任务的 public 方法
            citicBankCardSchedule.queryActiveCardTransactions();

            result.put("success", true);
            result.put("message", "定时任务逻辑已成功手动触发并执行完毕");

        } catch (Exception e) {
            ElgLogger.error(log, l -> l.error("手动触发定时任务时发生异常: {}", e.getMessage(), e));
            result.put("success", false);
            result.put("message", "手动触发失败: " + e.getMessage());
        }
        return result;
    }
}